using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Rapier.Kodachi.Core
{
    [Serializable]
    public class IndexFolder : INotifyPropertyChanged
    {
        private string _Path = String.Empty;
        public string Path
        {
            get 
            {
                if (_IsSpecialFolder)
                    return System.Environment.GetFolderPath((Environment.SpecialFolder)_SpecialFolder);
                else
                    return _Path; 
            }
            set 
            { 
                _Path = value;
                _IsSpecialFolder = false;
                OnPropertyChanged(new PropertyChangedEventArgs("Path"));
                OnPropertyChanged(new PropertyChangedEventArgs("IsSpecialFolder"));
            }
        }

        private bool _IsSpecialFolder;
        public bool IsSpecialFolder
        {
            get { return _IsSpecialFolder; }
            set
            { 
                _IsSpecialFolder = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IsSpecialFolder"));
            }
        }

        private int _SpecialFolder;
        public int SpecialFolder
        {
            get { return _SpecialFolder; }
            set 
            { 
                _SpecialFolder = value;
                OnPropertyChanged(new PropertyChangedEventArgs("SpecialFolder"));
            }
        }

        private BindingList<IndexExtension> _Extensions = new BindingList<IndexExtension>();
        public BindingList<IndexExtension> Extensions
        {
            get { return _Extensions; }
            set 
            { 
                _Extensions = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Extensions"));
            }
        }

        public IndexFolder()
        {
        }

        public IndexFolder(IndexFolder parentFolder)
        {
            _IsSpecialFolder = parentFolder.IsSpecialFolder;
            _SpecialFolder = parentFolder.SpecialFolder;
            _Path = parentFolder._Path;
            _Extensions = new BindingList<IndexExtension>(parentFolder.Extensions);
        }

        public IndexFolder(string path)
        {
            _Path = path;
        }

        public IndexFolder(Environment.SpecialFolder specialFolder)
        {
            _IsSpecialFolder = true;
            _SpecialFolder = (int)specialFolder;
        }

        public override string ToString()
        {
            if(_IsSpecialFolder)
                return GetSpecialFolderName();
            else
                return Path;
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(IndexFolder))
                return false;

            return Path.Equals((obj as IndexFolder).Path);
        }

        public override int GetHashCode()
        {
            return Path.GetHashCode();
        }

        private string GetSpecialFolderName()
        {
            switch (_SpecialFolder)
            {
                case (int)Environment.SpecialFolder.StartMenu:
                    return "Start Menu";
                case (int)Environment.SpecialFolder.MyDocuments:
                    return "Documents";
                case (int)Environment.SpecialFolder.Desktop:
                    return "Desktop";
                case (int)Environment.SpecialFolder.Favorites:
                    return "Favorites";
                default:
                    return String.Empty;
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
    }
}
